public class RomanToNumber {
    //MMMDMCCLIII
    public static void main(String[] args)
    {
        char [] RomArray = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};
        int[] NumArray = new int[] {1, 5, 10, 50, 100, 500, 1000};
        int num=0,x=0;
        String Roman = "MMMCMLXIV"; //3964
        char[] charArray = Roman.toCharArray();
        for (int i=charArray.length-1; i>=0;i--)
        {
            for (int j=0; j<RomArray.length;j++)
            {
                if(charArray[i] == RomArray[j])
                {
                    num+=NumArray[j];
                    if(x>j)
                    {
                        num-=2*NumArray[j];
                    }
                    x=j;
                }
            }
        }
        System.out.println(Roman + " = " +num);
    }
}
